//
//  AudioBookDetailViewController.m
//  FunRead
//
//  Created by  King on 15/5/27.
//  Copyright (c) 2015年 华东伟. All rights reserved.
//

#import "AudioBookDetailViewController.h"
#import "SANetWorkingTask.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "AudioBookDetailCell.h"
#import "AudioBookDetailModel.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "AudioBookPlayViewController.h"

@interface AudioBookDetailViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, retain) NSMutableArray *collectionArray;
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) UIView *HeaderView;
@property (nonatomic, retain) NSMutableDictionary *headerDic;
@property (nonatomic, retain) UIView *ContentView;
@property (nonatomic, retain) NSMutableArray *titleArray;
@property (nonatomic, retain) UILabel *contentLabel;
@end

@implementation AudioBookDetailViewController
- (void)dealloc
{
    [_Id release];
    [_name release];
    [_collectionArray release];
    [_collectionView release];
    [_HeaderView release];
    [_headerDic release];
    [_ContentView release];
    [_URLArray release];
    [_titleArray release];
    [_contentLabel release];
    [super dealloc];
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.collectionArray = [NSMutableArray array];
        self.URLArray = [NSMutableArray array];
        self.titleArray = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // 添加背景图片
    UIImageView *BackImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    BackImage.image = [UIImage imageNamed:@"1.jpg"];
    [self.view addSubview:BackImage];
    [BackImage release];
    
    
    // 设置导航返回键图片
    UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"backbefore.png"] style:UIBarButtonItemStylePlain target:self action:@selector(action:)];
    self.navigationItem.leftBarButtonItem = button;
    [button release];
    
    self.navigationItem.title = self.name;
    [self startConnection];
    
}
- (void)action:(id)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 网络请求
- (void)startConnection
{
    __block AudioBookDetailViewController *detail = self;
    NSString *str = [NSString stringWithFormat:@"http://app.9nali.com/961/albums/%@?page_id=1&isAsc=true&device=iPad&version=1.1.2", self.Id];
    [SANetWorkingTask request:str parmater:nil block:^(NSDictionary *result) {
        detail.headerDic = [result objectForKey:@"album"];
        
        NSDictionary *dic = [result objectForKey:@"tracks"];
        detail.collectionArray = [dic objectForKey:@"list"];

        [self URLSave];
        [_collectionView reloadData];
        [self createHeaderView];
        
    }];
}
// 将play.mp3文件存放到URL并传递URL
- (void)URLSave
{


    for (NSDictionary *dic in _collectionArray) {
        [self.URLArray addObject:[dic objectForKey:@"playUrl32"]];
        [self.titleArray addObject:[dic objectForKey:@"title"]];
        
    }
    
}


#pragma mark - 建立HeaderView
- (void)createHeaderView
{
    // 设置当前页面背景图片
    UIImageView *backImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [backImage sd_setImageWithURL:[NSURL URLWithString:[self.headerDic objectForKey:@"coverLarge"]]placeholderImage:[UIImage imageNamed:@"1.jpg"]];
    [self.view addSubview:backImage];
    [backImage release];
    
    
    self.HeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 6.0)];
    _HeaderView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
    // HeaderView上添加Header信息
    UIImageView *HeaderImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, self.HeaderView.frame.size.height - 20, self.HeaderView.frame.size.height - 20)];
    [HeaderImage sd_setImageWithURL:[NSURL URLWithString:[self.headerDic objectForKey:@"coverSmall"]]];placeholderImage:[UIImage imageNamed:@"1.jpg"];
    [_HeaderView addSubview:HeaderImage];
    [HeaderImage release];
    
    UIImageView *smallLogo = [[UIImageView alloc] initWithFrame:CGRectMake(self.HeaderView.frame.size.height - 20 + 20, 12, 20, 20)];
    
    [smallLogo sd_setImageWithURL:[NSURL URLWithString:[_headerDic objectForKey:@"smallLogo"]] placeholderImage:[UIImage imageNamed:@"1.jpg"]];
    smallLogo.layer.cornerRadius = 10;
    smallLogo.layer.masksToBounds = YES;
    [_HeaderView addSubview:smallLogo];
    [smallLogo release];
    
    UILabel *nickname = [[UILabel alloc] initWithFrame:CGRectMake(self.HeaderView.frame.size.height - 20 + 45, 10, self.view.frame.size.width, (self.HeaderView.frame.size.height - 20) / 3.0)];
    nickname.text = [NSString stringWithFormat:@"%@  发布", [self.headerDic objectForKey:@"nickname"]];
    nickname.textColor = [UIColor whiteColor];
    [_HeaderView addSubview:nickname];
    [nickname release];
    
    UILabel *tracks = [[UILabel alloc] initWithFrame:CGRectMake(self.HeaderView.frame.size.height - 20 + 30, (self.HeaderView.frame.size.height - 20) / 3.0 + 10 + 10, self.view.frame.size.width, (self.HeaderView.frame.size.height - 20) / 3.0)];
    tracks.textColor = [UIColor whiteColor];
    tracks.text = [NSString stringWithFormat:@"节目数：%@", [_headerDic objectForKey:@"tracks"]];
    [_HeaderView addSubview:tracks];
    [tracks release];
    
    [self.view addSubview:_HeaderView];
    [_HeaderView release];
    
    
    NSArray *segmentedArray = [[NSArray alloc]initWithObjects:@"列表",@"简介",nil];
    //初始化UISegmentedControl
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentedArray];
    segmentedControl.frame = CGRectMake(0, self.view.frame.size.height / 6.0, self.view.frame.size.width, 50.0);
    segmentedControl.tintColor = [UIColor lightGrayColor];

    segmentedControl.backgroundColor = [UIColor whiteColor];
    segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;//设置样式
    // 点击后是否恢复原样
    segmentedControl.momentary = YES;
    [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:segmentedControl];
    [segmentedControl release];
    [self createCollectView];
}
// segenment点击方法
- (void)segmentAction:(UISegmentedControl *)seg
{
    switch (seg.selectedSegmentIndex) {
        case 0:
        {
            [self createCollectView];
        }
            break;
        case 1:
        {
            [self createContentView];
        }
            break;
        default:
            break;
    }
}

#pragma mark - 建立CollectView
- (void)createCollectView
{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.sectionInset = UIEdgeInsetsMake(10, 0, 10, 0);
    
    layout.itemSize = CGSizeMake(self.view.frame.size.width , (self.view.frame.size.height) / 10);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height / 6.0 + 50, self.view.frame.size.width, self.view.frame.size.height  - (self.view.frame.size.height / 6.0 + 50)) collectionViewLayout:layout];
    // 设置代理
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    // 注册cellectionView重用池
    [_collectionView registerClass:[AudioBookDetailCell class] forCellWithReuseIdentifier:@"cell"];
    _collectionView.backgroundColor = [UIColor lightGrayColor];
    
    
    [self.view addSubview:_collectionView];
    
    [layout release];
    [_collectionView release];
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.collectionArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    AudioBookDetailCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    NSDictionary *dic = [self.collectionArray objectAtIndex:indexPath.row];
    AudioBookDetailModel *model = [[AudioBookDetailModel alloc] init];
    [model setValuesForKeysWithDictionary:dic];
    [cell.coverSmall sd_setImageWithURL:[NSURL URLWithString:model.coverSmall] placeholderImage:[UIImage imageNamed:@"1.jpg"]];
    cell.title.text = model.title;
    cell.playtimes.text = [NSString stringWithFormat:@"▶️%@",model.playtimes];
    // 将string类型转换int型，并计算时间
    int duration = [model.duration intValue];
    int minute = duration / 60;
    int second = duration % 60;
    cell.duration.text = [NSString stringWithFormat:@"🕒%d:%d", minute, second];
    [model release];
    
    
    
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    AudioBookPlayViewController *play = [[AudioBookPlayViewController alloc] init];
    AudioBookDetailModel *detailModel = [[AudioBookDetailModel alloc] init];
    NSDictionary *dic = [_collectionArray objectAtIndex:indexPath.row];
    [detailModel setValuesForKeysWithDictionary:dic];
    
    play.trackId = detailModel.trackId;
    play.name = detailModel.title;
    play.playImage = detailModel.coverLarge;
    play.URLArray = self.URLArray;
    play.titleArray = self.titleArray;
    play.page = indexPath.row;

    [play play];
    [self.navigationController pushViewController:play animated:YES];
    [detailModel release];
    [play release];
}
#pragma mark - 建立ContentView
- (void)createContentView
{
    self.ContentView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height / 6.0 + 50, self.view.frame.size.width, self.view.frame.size.height - 64 - 49)];
    _ContentView.backgroundColor = [UIColor whiteColor];
    
    self.contentLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    _contentLabel.numberOfLines = 0;
    _contentLabel.textColor = [UIColor grayColor];
    _contentLabel.text = [NSString stringWithFormat:@"    %@......", [_headerDic objectForKey:@"intro"]];
    [_ContentView addSubview:_contentLabel];
    [_contentLabel release];
    
    
    NSString *str = self.contentLabel.text;
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
    CGRect rect = [str boundingRectWithSize:CGSizeMake(self.view.frame.size.width - 20, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    self.contentLabel.frame = CGRectMake(10, 10, self.view.frame.size.width - 20, rect.size.height);

    
    [self.view addSubview:_ContentView];
    [_ContentView release];
    
}

    



@end
